package cfca.seal.web.system;

import cfca.itool.util.GUID;
import cfca.itool.util.StringUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionToken
{
  private static final String TOKEN = "token";

  public static String generateToken(HttpServletRequest request)
  {
    String token = GUID.getTxNo();
    request.getSession().setAttribute("token", token);
    return token;
  }

  public static String getToken(HttpServletRequest request) {
    return request.getSession().getAttribute("token").toString();
  }

  public static boolean validateToken(HttpServletRequest request, String token) {
    if ((StringUtil.isNotEmpty(token)) && (token.equals(request.getSession().getAttribute("token").toString()))) {
      generateToken(request);
      return true;
    }
    return false;
  }
}